package com.fh.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.utils.bean.*;
import com.fh.mapper.*;
import com.fh.service.RegisterService;
import com.utils.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author whf
 * @date 2023年04月24日 15:17
 */
@Service
public class RegisterServiceImpl implements RegisterService {




    @Autowired
    private UserMapper userMapper;
    @Autowired
    private SafetyMapper safetyMapper;

    @Autowired
    private SafetyoruserMapper safetyoruserMapper;

    @Autowired
    private RanduMapper randuMapper;



    @Autowired
    private RoleMapper roleMapper;


    /**
     * @param safety
     * @return java.lang.Object
     * @description: 进行账号登录 密码判断
     * @author whf
     * @date 2023/4/25 8:29
     */
    @Override
    public Safety getSafety(Safety safety) {
        QueryWrapper<Safety> safetyQueryWrapper = new QueryWrapper<>();
//        先去根据账号得到 数据库中的密码 然后去进行密码对比
        safetyQueryWrapper.eq("accountname", safety.getAccountname());
        Safety safetySql = safetyMapper.selectOne(safetyQueryWrapper);
        return safetySql;
    }

    /**
     * @description: 密码正确就会对个人信息进行查询
     * @author whf
     * @date 2023/5/10 14:12
     * @param safety
     * @return com.utils.bean.User
     */
    @Override
    public User LoginJudgment(Safety safety) {
        User user = safetyMapper.getUser(safety.getAccountname());
        return user;
    }

    /**
     * @param substitute
     * @return java.util.List<com.fh.bean.Resource>
     * @description: 通过个人的Id得到他的职位信息
     * @author whf
     * @date 2023/4/26 8:28
     */
    @Override
    public List<Role> selectUID(Substitute substitute) {
        List<Role> roles = roleMapper.selectUID(Long.valueOf(substitute.getId()));
        return roles;
    }



    /**
     * @param user
     * @description: 注册接口对个人信息表表进行添加
     * @author whf
     * @date 2023/5/10 14:38
     */
    @Override
    public User setUser(User user) {
        //       查询有没有改手机号 如果有这个手机号就不允许注册
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("phone", user.getPhone());
        User userOne = userMapper.selectOne(userQueryWrapper);
        if(userOne != null && ObjectUtil.isNotNull(userOne)) {
            throw new BusinessException("该手机号已注册");
        }
        //        添加到数据库 主键自动回填
        userMapper.insert(user);
        return user;
    }

    /**
     * @param safety
     * @return com.utils.bean.Safety
     * @description: 对账号密码进行保存
     * @author whf
     * @date 2023/5/10 14:41
     */
    @Override
    public Safety setSafety(Safety safety) {
        //        查询账号有没有被注册 如果账号被注册 那么账号也不允许注册
        QueryWrapper<Safety> safetyQueryWrapper = new QueryWrapper<>();
        safetyQueryWrapper.eq("accountname", safety.getAccountname());
        Safety safetyOne = safetyMapper.selectOne(safetyQueryWrapper);
        if(safetyOne != null && ObjectUtil.isNotNull(safetyOne)) {
            throw new BusinessException("该账号已注册");
        }
        //        添加到数据库 主键自动回填
        safetyMapper.insert(safety);
        return safety;
    }

    /**
     * @description: 对用户跟账号的表进行关联
     * @author whf
     * @date 2023/5/10 14:50
     */
    @Override
    public void setSafetyoruser(Safetyoruser safetyoruser) {
        //        对表关联进行添加
        safetyoruserMapper.insert(safetyoruser);
//        对职位表进行关联 默认为2 后期可以修改
        Randu randu = new Randu(safetyoruser.getUsid(), 2);
        randuMapper.insert(randu);
    }




}
